<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2012 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class b2c_tasks_order_delivery extends base_task_abstract implements base_interface_task{

    function exec($params=null)
    {
        $delivery_mdl = app::get('b2c')->model('order_delivery_time');
        $orders = $delivery_mdl->getList("*",array('delivery_time|sthan'=>time()));
        $this->delivery_orders($orders);
    }

    function delivery_orders($orders)
    {
        $order = app::get('b2c')->model('orders');
        $delivery_mdl = app::get('b2c')->model('order_delivery_time');
        foreach($orders as $val){
            $sdf = array(
                'order_id' => $val['order_id'],
                'received_time' => $val['delivery_time'],
                'received_status' =>1,
            );
//            if($order->save($sdf)){////add by hzk 06-12 结算积分   
            if($order->save($sdf)){
	            if($this->receive_add_point($val['order_id'])){
	                $delivery_mdl->delete(array('order_id' => $val['order_id']));
	            }////add by hzk 06-12 结算积分   
            }
        }
    }
        //add by hzk 06-12 增加  确认收货时，结算积分    
    function receive_add_point($order_id){
		$arr_member_info = kernel::single('b2c_user_object')->get_current_member();
		$member_id = $arr_member_info['member_id']; 
//		$memberId = intval($this->app->member_id);//会员id号
    	$order =  app::get('b2c')->model('orders');
    	$subsdf = array('order_objects'=>array('*',array('order_items'=>array('*',array(':products'=>'*')))));
    	$sdf_order = $order->dump($order_id,'*',$subsdf);
    	 // 订单积分结算埋点
        $policy_stage = app::get('b2c')->getConf("site.get_policy.stage");
//        if ($sdf_order['received_status'] == '1' && $policy_stage =='4')
//        if ($sdf_order['ship_status'] == '1' && $policy_stage == '4')//去掉确认收货的判断因为，这里是自动确认收货
        if ($sdf_order['received_status'] == '1' && $sdf_order['ship_status'] == '1' && $policy_stage == '4')
            $stage = '1';
        else
            $stage = '0';

    	    $reason = 'receive';
    	    $msg = '自动--确认收货时结算积分';
        // 获得积分
        $obj_add_point = kernel::service('b2c_member_point_add');
        if ($stage){
            $obj_add_point->change_point($sdf_order['member_id'], intval($sdf_order['score_g']), $msg, 'order_pay_get', 2, $stage, $order_id, '', $reason);
//            $obj_add_point->change_point($sdf_order['member_id'], intval($sdf_order['score_g']), $msg, 'order_pay_get', 2, $stage, $order_id, $member_id, $reason);
    	return true;
        }
    	
    	return false;
    }//add end
    
}

